import { getPath } from "./index.js";
import shelljs from "shelljs";
import { getSfduFromSdp } from "./lanzou-helper.js";
import { getGitReleaseDownUrl } from "./github-helper.js";
import { getKuAnDownUrl } from "./kuan-helper.js";
import { getApkComboDownUrl } from "./apkcombo-helper.js";


const { rootPath, path } = getPath();
const getRealDwonUrls = async (shares) => {
  const realDwonUrl = [];
  for (const iterator of shares) {
    if (iterator.sharePage.indexOf("lanzouw") > -1 || iterator.sharePage.indexOf("lanzoux") > -1) {
      const realDwonUrls = await getSfduFromSdp(
        iterator.sharePage,
        iterator.fileNames?.map((it) => it.searchName)
      );
      for (const [index, url] of realDwonUrls.entries()) {
        realDwonUrl.push({
          ...url,
          savedName: iterator.fileNames[index].savedName,
        });
      }
    } else if (iterator.sharePage.indexOf("github") > -1) {
      const url = await getGitReleaseDownUrl(iterator.sharePage, iterator.fileNames[0]?.searchName);
      realDwonUrl.push({
        ...url,
        savedName: iterator.fileNames[0].savedName,
      });
    } else if (iterator.sharePage.indexOf("coolapk") > -1) {
      const url = await getKuAnDownUrl(iterator.sharePage);
      realDwonUrl.push({
        ...url,
        savedName: iterator.fileNames[0].savedName,
      });
    } else if(iterator.sharePage.indexOf("apkcombo")>-1){
      const url = await getApkComboDownUrl(iterator.sharePage);
      realDwonUrl.push({
        ...url,
        savedName: iterator.fileNames[0].savedName,
      });
    } else {
      realDwonUrl.push({
        url: iterator.sharePage,
        savedName: iterator.fileNames[0].savedName,
      });
    }
  }
  return realDwonUrl;
};

const files = [
  // {
  //   sharePage: "https://qiqi2020.lanzouw.com/b09svqv1c",
  //   fileNames: [
  //     {
  //       searchName: "影视仓_",
  //       savedName: "tvbox.apk",
  //     },
  //     {
  //       searchName: "影视仓手机版_",
  //       savedName: "tvbox-mobile.apk",
  //     },
  //   ],
  // },
  // {
  //   sharePage: "https://lanzoux.com/adWars",
  //   fileNames: [{ savedName: "adwars.apk" }],
  // },
  {
    sharePage: "https://github.com/lyswhut/lx-music-mobile",
    fileNames: [{ savedName: "lx-music.apk" }],
  },
  {
    sharePage: "https://github.com/Kr328/ClashForAndroid",
    fileNames: [{ savedName: "clash.apk" }],
  },
  {
    sharePage: "https://github.com/rustdesk/rustdesk/releases",
    fileNames: [{ searchName: "-aarch64-signed.apk", savedName: "rustdesk.apk" }],
  },

  {
    sharePage: "https://github.com/eprendre/tingshu",
    fileNames: [{ savedName: "tingshu.apk" }],
  },
  {
    sharePage: "https://apkcombo.com/zh/google-play-store/com.android.vending/download/apk",
    fileNames: [{ savedName: "google-play.apk" }],
  },
  // {
  //     sharePage: "https://www.coolapk.com/apk/mark.via",
  //   fileNames: [{ savedName: "via.apk" }],
  // },
  // {
  //   sharePage: "https://www.coolapk.com/apk/com.goplaycn.googleinstall",
  //   fileNames: [{ savedName: "goplay.apk" }],
  // },
  // {
  //   sharePage: "https://www.coolapk.com/apk/io.legado.app.release",
  //   fileNames: [{ savedName: "legado.apk" }],
  // },
 
];

const main = async () => {
  console.log("定时任务主程序 启动即执行一次");
  const realDwonUrls = await getRealDwonUrls(files);
  for (const realDwonUrl of realDwonUrls) {
    let curlStr = `curl --location '${realDwonUrl.url}' --output '${path.join(rootPath, "public/" + realDwonUrl.savedName)}'`;
    if (realDwonUrl.header) {
      curlStr = curlStr + ` --header ${realDwonUrl.header}`;
    }
    if (realDwonUrl.cookies) {
      curlStr = curlStr + ` --header 'Cookie: ${realDwonUrl.cookies}'`;
    }
    if(realDwonUrl.otherCurlParams){
      curlStr = curlStr + ` ${realDwonUrl.otherCurlParams}`;
    }
    console.log(`${realDwonUrl.savedName}下载中---`);
    shelljs.exec(curlStr);
  }
};

export const runTask = () => {
  console.log("定时任务已启动");
  main();
  setInterval(() => {
    main();
  }, 1000 * 60 * 60 * 3);
};
